package com.androwit.engine.mapping.impl;

import java.util.Map;

import com.androwit.engine.mapping.MappingField;
import com.androwit.engine.mapping.MappingFieldsDefiner;

/**
 * A basic implementation with constant fields
 * 
 * @author Zied Hamdi - http://1vu.fr
 * 
 * @param <J>
 * @param <K>
 * @param <V>
 */
public class BasicMappingFieldsDefiner<J, K, V> implements MappingFieldsDefiner<J, K, V> {

	protected Class<?> clazz;
	protected MappingField<J, K, V>[] fields;

	/**
	 * no comment
	 * 
	 * @param clazz
	 *          must have an empty public constructor
	 * @param fields
	 *          the fields of clazz
	 */
	public BasicMappingFieldsDefiner(Class<?> clazz, MappingField<J, K, V>[] fields) {
		this.fields = fields;
		this.clazz = clazz;
	}

	@Override
	public MappingField<J, K, V>[] getMappedFields(Map<K, V> values) {
		return fields;
	}

	@Override
	public Object createInstance(Map<K, V> values) {
		try {
			return clazz.newInstance();
		} catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	@Override
	public MappingField<J, K, V>[] getMappingFields(Object toPersist) {
		return fields;
	}

	public MappingField<J, K, V>[] getFields() {
		return fields;
	}

	public void setFields(MappingField<J, K, V>[] fields) {
		this.fields = fields;
	}

	public Class<?> getClazz() {
		return clazz;
	}

	public void setClazz(Class<?> clazz) {
		this.clazz = clazz;
	}

}
